/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-11-05
 * Time: 15:28
 */

public class MyCircularQueueL {
    public int[] elem = new int[3];
    public int head;
    public int tail;
    public int size;

    //搞一个专门的锁对象
    Object locker = new Object();

    //入队列
    public void push(int val) throws InterruptedException {
      synchronized (locker){
          //判断满不满
          if (elem.length == size){
              locker.wait();
          }
          elem[tail] = val;
          tail++;
          //处理tail走到数组末尾的情况
          if (tail >= elem.length){
              tail = 0;
          }
          size++;
          locker.notify();
      }
    }

    //出队列
    public Integer pop() throws InterruptedException {
        synchronized (locker){
            //判断空不空
            if (size == 0){
                locker.wait();
            }
            int ret = elem[head];
            head++;
            if (head == elem.length){
                head = 0;
            }
            size--;
            locker.notify();
            return ret;
        }
    }

}
